<template>
  <div class="list">
    <el-table :data="tableData" style="width: 100%">
      <el-table-column prop="name" label="姓名"></el-table-column>
      <el-table-column prop="Gender" label="性别"></el-table-column>
      <el-table-column prop="fansNum" label="粉丝"></el-table-column>
      <el-table-column fixed="right" label="操作" width="250">
        <template slot-scope="scope">
          <el-button type="text" @click="lookClick(scope.$index)" size="small"
            >查看</el-button
          >
          <el-button type="text" @click="updateClick(scope.$index)" size="small"
            >编辑</el-button
          >
          <el-button type="text" @click="delClick(scope.$index)" size="small"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-dialog title="教师信息" :visible.sync="dialogFormVisible">
      <el-form :model="form" :disabled="true">
        <el-form-item label="姓名:" :label-width="formLabelWidth">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="性别:" :label-width="formLabelWidth">
          <el-input v-model="form.Gender" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="粉丝：" :label-width="formLabelWidth">
          <el-input v-model="form.fansNum" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="dialogFormVisible = false"
          >确 定</el-button
        >
      </div>
    </el-dialog>

    <el-dialog title="修改详情" :visible.sync="dialogFormVisibleU">
      <el-form :model="form" ref="updateForm" :rules="rules">
        <el-form-item label="姓名:" prop="name" :label-width="formLabelWidth">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item
          label="性别："
          prop="Gender"
          :label-width="formLabelWidth"
        >
          <el-select v-model="form.Gender" placeholder="请选择性别">
            <el-option
              v-for="(teacher, index) in taecherS"
              :key="index"
              :label="teacher.Gender"
              :value="teacher.Gender"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisibleU = false">取 消</el-button>
        <el-button type="primary" @click="updateClickLink">保 存</el-button>
      </div>
    </el-dialog>
    <el-dialog title="添加老师" :visible.sync="dialogFormVisibleAdd">
      <el-form ref="addForm" :rules="rules" :model="form">
        <el-form-item label="姓名:" prop="name" :label-width="formLabelWidth">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item
          label="性别："
          prop="Gender"
          :label-width="formLabelWidth"
        >
          <el-select v-model="form.Gender" placeholder="请选择性别">
            <el-option
              v-for="(teacher, index) in taecherS"
              :key="index"
              :label="teacher.Gender"
              :value="teacher.Gender"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisibleAdd = false">取 消</el-button>
        <el-button type="primary" @click="addClick">保 存</el-button>
      </div>
    </el-dialog>
    <div style="position: absolute; top: 20px; right: 30px; z-index: 1000">
      <el-button type="button" @click="dialogFormVisibleAdd = true"
        >添加教课老师</el-button
      >
    </div>
  </div>
</template>
<script>
import { courses, taechers } from "../../../public/schoolData.js";
export default {
  name: "teacherList",
  data() {
    return {
      dialogFormVisible: false,
      dialogFormVisibleU: false,
      dialogFormVisibleAdd: false,
      tableData: taechers,
      form: {
        id: "",
        name: "",
        Gender: "",
        fansNum: 0,
      },
      formLabelWidth: "120px",
      handleIndex: 0,
      taecherS: taechers,
      rules: {
        name: [
          { required: true, message: "姓名不可为空", trigger: "blur" },
          {
            min: 2,
            max: 10,
            message: "长度在 2 到 6 个字符",
            trigger: "blur",
          },
        ],
        Gender: [{ required: true, message: "请选择性别", trigger: "blur" }],
      },
    };
  },
  watch: {
    dialogFormVisible(newVal) {
      if (!newVal) (this.form.name = ""), (this.form.Gender = "");
    },
    dialogFormVisibleU(newVal) {
      if (!newVal) {
        (this.form.name = ""), (this.form.info = "");
      }
    },
  },
  methods: {
    lookClick(index) {
      this.handleIndex = index;
      this.form.id = taechers[index].id;
      this.form.name = taechers[index].name;
      this.form.Gender = taechers[index].Gender;
      this.form.fansNum = taechers[index].fansNum;
      this.dialogFormVisible = true;
    },
    updateClick(index) {
      this.handleIndex = index;
      this.dialogFormVisibleU = true;
      this.form.id = taechers[index].id;
      this.form.name = taechers[index].name;
      this.form.Gender = taechers[index].Gender;
      this.form.fansNum = taechers[index].fansNum;
    },
    updateClickLink() {
      this.$refs["updateForm"].validate((valid) => {
        if (valid) {
          taechers[this.handleIndex].name = this.form.name;
          taechers[this.handleIndex].Gender = this.form.Gender;
          this.dialogFormVisibleU = false;
        }
      });
    },
    addClick() {
      this.$refs["addForm"].validate((valid) => {
        if (valid) {
          let tId = 1;
          if (taechers.length > 0) {
            tId = taechers[taechers.length - 1].id + 1;
          } else {
            tId = 1;
          }
          taechers.push({
            id: tId,
            name: this.form.name,
            type: "关注",
            Gender: this.form.Gender,
            fansNum: 0,
            inCourseList: [],
          });
          this.dialogFormVisibleAdd = false;
        }
      });
    },
    delClick(index) {
      taechers.splice(index, 1);
    },
  },
};
</script>

<style scoped>
</style>